from django.db import models
from libs.mixins import ModelMixin
from libs.utils import human_time
import json


class Wechat(models.Model, ModelMixin):
    openid = models.CharField(max_length=100, unique=True, null=True)  # 微信openid
    session_key = models.CharField(max_length=100, null=True)  # session_key
    nickname = models.CharField(max_length=100, null=True)  # 昵称
    mobile = models.CharField(max_length=20, unique=True, null=True)  # 手机
    avatar = models.CharField(max_length=255, null=True)  # 微信头像
    region = models.CharField(max_length=255, null=True)  # 用户地区
    gender = models.IntegerField(null=True)  # 性别
    access_token = models.CharField(max_length=32, null=True)  # 用于认证的token
    token_expired = models.IntegerField(null=True)  # token的过期时间
    create_date = models.CharField(max_length=20, default=human_time)  # 创建时间

    class Meta:
        db_table = 'wechat_user'
        ordering = ('-id',)

    def __repr__(self):
        return '<Wechat %r>' % self.mobile

    def to_cache(self):
        return json.dumps(super().to_dict(selects=('id', 'openid', 'token_expired')))

